package lsl.leetCode.year2023.test0202;

/**
 * @author Zed
 * @description
 * @date 2023/2/1 23:43
 */
//二分查找
public class test704 {
    public static void main(String[] args) {
        int[] nums = {-1, 0, 3, 5, 9, 12,14,15,18,19};
        System.out.println(search(nums, 14));
    }

    public static int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1; // 注意
            }
        }
        return -1;
    }
}
